using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using System.Configuration;
using System.Web.Configuration;
using Config = System.Configuration.Configuration;
using Identity = System.Web.Configuration.IdentitySection;

namespace Microsoft.Practices.RepositoryFactory.Actions.Configuration
{
    /// <summary>
    /// Action thats set the connection string entry on the Configuration File
    /// </summary>
    public class SetConnectionStringEntryAction : ConfigurableAction
    {
        #region Input Properties
        Config configuration;
        /// <summary>
        /// Gets or sets the configuration.
        /// </summary>
        /// <value>The configuration.</value>
        [Input(Required = true)]
        public Config Configuration
        {
            get { return configuration; }
            set { configuration = value; }
        }

        private string connectionStringEntryName;

        /// <summary>
        /// Gets or sets the name of the connection string entry.
        /// </summary>
        /// <value>The name of the connection string entry.</value>
        [Input(Required = true)]
        public string ConnectionStringEntryName
        {
            get { return connectionStringEntryName; }
            set { connectionStringEntryName = value; }
        }

        private string connectionStringEntryValue;

        /// <summary>
        /// Gets or sets the connection string entry value.
        /// </summary>
        /// <value>The connection string entry value.</value>
        [Input(Required = true)]
        public string ConnectionStringEntryValue
        {
            get { return connectionStringEntryValue; }
            set { connectionStringEntryValue = value; }
        }

        private string providerName;

        /// <summary>
        /// Gets or sets the name of the provider.
        /// </summary>
        /// <value>The name of the provider.</value>
        [Input(Required = true)]
        public string ProviderName
        {
            get { return providerName; }
            set { providerName = value; }
        }
        #endregion

        #region Action Implementation
        /// <summary>
        /// See <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Execute"/>.
        /// </summary>
        public override void Execute()
        {
            ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)configuration.GetSection("connectionStrings");

            if(connectionStringsSection.ConnectionStrings[connectionStringEntryName] == null)
            {
                //Add Entry
                connectionStringsSection.ConnectionStrings.Add(
                    new ConnectionStringSettings(
                        connectionStringEntryName, 
                        connectionStringEntryValue,
                        providerName));
            }
            else
            { 
                //Update Entry
                connectionStringsSection.ConnectionStrings[connectionStringEntryName].ConnectionString = 
                    ConnectionStringEntryValue;
            }
        }

        /// <summary>
        /// See <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Undo"/>.
        /// </summary>
        public override void Undo()
        {
            //Not Implemented
        } 
        #endregion
    }
}
